<template>
  <div class="scroll-view" ref="scroll">
    <div class="scroll-wrap" :style="{ width: wrapWidth }">
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    change: Function,
    scrollX: {
      type: Boolean,
      default: false,
    },
    scrollY: {
      type: Boolean,
      default: true,
    },
    width: {
      type: [String, Number],
      default: "100%",
    },
  },
  computed: {
    wrapWidth() {
      if (typeof this.width === "string") {
        return this.width;
      } else {
        return this.width + "px";
      }
    },
  },
  mounted() {
    const scroll = new IScroll(this.$refs.scroll, {
      click: true,
      tap: true,
      probeType: 3,
      scrollX: this.scrollX,
      scrollY: this.scrollY,
    });
    scroll.on("beforeScrollStart", () => {
      scroll.refresh();
    });
    // 监听外面的数据
    this.change &&
      scroll.on("scroll", () => {
        if (scroll.y >= 0) {
          // 往下拉
          this.change(true);
        } else {
          this.change(false);
        }
      });
  },
};
</script>

<style>
.scroll-wrap {
  overflow: hidden;
}
</style>
